package com.car.advertising.view;

/**
 * 开发者：LuoYi
 * 带进度条的WebView
 * Time: 2017 13:56 2017/3/13 03
 */

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.GeolocationPermissions;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.car.advertising.R;

import static android.content.Context.MODE_PRIVATE;


@SuppressWarnings("deprecation")
public class ProgressWebView extends WebView {

    private ProgressBar progressbar;

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
        progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 10, 0, 0));

        Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states);
        progressbar.setProgressDrawable(drawable);
        addView(progressbar);
        setWebChromeClient(new WebChromeClient());
        //是否可以缩放
        getSettings().setSupportZoom(true);
        getSettings().setBuiltInZoomControls(true);

        // 清楚缓存
        // webView.clearCache(true);
        // webView.clearFormData();
        // 清楚历史记录
        // webView.clearHistory();
        // 可以纵向滚动
        setVerticalScrollBarEnabled(true);
        // 可以横向滚动
        setHorizontalScrollBarEnabled(false);
        // 设置缓冲的模式
        getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        getSettings().setSaveFormData(false);
        getSettings().setSavePassword(false);
        // 设置支持JavaScript脚本
        getSettings().setAppCacheMaxSize(8 * 1024 * 1024);// 缓存最多可以有8M
        getSettings().setAllowFileAccess(true);// 可以读取文件缓存(manifest生效)
        getSettings().setAppCacheEnabled(true);// 应用可以有缓存
        getSettings().setDatabaseEnabled(true);
        String dir = context.getDir("database", MODE_PRIVATE).getPath();
        getSettings().setJavaScriptEnabled(true);
        getSettings().setGeolocationEnabled(true);
        getSettings().setGeolocationDatabasePath(dir);
        getSettings().setDomStorageEnabled(true);// 设置可以使用localStorage
        setDownloadListener(new MyWebViewDownLoadListener());
    }

    public class WebChromeClient extends android.webkit.WebChromeClient {

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
            super.onGeolocationPermissionsShowPrompt(origin, callback);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(View.INVISIBLE);
            } else {
                if (View.INVISIBLE == progressbar.getVisibility()) {
                    progressbar.setVisibility(View.VISIBLE);
                }
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

    }

    // 为webview配置下载功能
    private class MyWebViewDownLoadListener implements DownloadListener {

        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            System.out.println(url);
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
        lp.x = l;
        lp.y = t;
        progressbar.setLayoutParams(lp);
        super.onScrollChanged(l, t, oldl, oldt);
    }


}
